package com.liu.soundCode.Arrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Stream;

/**
 * @author liubo
 */
public class ArraysUtils {

    private static int[] arr = new int[6];
    private static String[] strArray = {"2", "5", "0", "4", "6", "-10"};
    private static int[] oneDimensional = {2, 5, 0, 4, 6, -10};
    private static int[][] twoDimensional = {{2, 5}, {0, 4}, {6, -10}};

    public static void main(String[] args) {
        System.out.println("--------------------数组覆写值--------------------");
        Arrays.fill(arr, 1);
        System.out.println(Arrays.toString(arr));

        arr = new int[6];
        Arrays.fill(arr, 0, arr.length, 2);
        System.out.println(Arrays.toString(arr));

        System.out.println("--------------------数组复制--------------------");
        System.out.println("1)System.arraycopy()。效率最高");
        int[] arraycopy = new int[6];
        System.arraycopy(oneDimensional, 0, arraycopy, 0, arr.length);
        System.out.println(Arrays.toString(arraycopy));


        System.out.println("2)clone()");
        int[] clone = oneDimensional.clone();
        System.out.println(Arrays.toString(clone));

        System.out.println("3)Arrays.copyof()");
        int[] copyOf = Arrays.copyOf(oneDimensional, oneDimensional.length);
        System.out.println(Arrays.toString(copyOf));

        System.out.println("4)Arrays.copyofRange()");
        int[] copyOfRange = Arrays.copyOfRange(oneDimensional, 0, oneDimensional.length);
        System.out.println(Arrays.toString(copyOfRange));

        System.out.println("--------------------数组排序--------------------");
        System.out.println(Arrays.toString(oneDimensional));
        Arrays.sort(oneDimensional);
        System.out.println(Arrays.toString(oneDimensional));

        System.out.println("--------------------数组查询--------------------");
        System.out.println("数组必须先进行sort排序；查询key的下标，没有则为-1");
        Arrays.sort(oneDimensional);
        int index = Arrays.binarySearch(oneDimensional, 5);
        System.out.println(oneDimensional[index]);


        System.out.println("--------------------数组类型转化--------------------");
        int[] strToInt1 = Arrays.stream(strArray).mapToInt(Integer::parseInt).toArray();
        int[] strToInt2 = Stream.of(strArray).mapToInt(Integer::parseInt).toArray();
        System.out.println(Arrays.toString(strToInt1));
        System.out.println(Arrays.toString(strToInt2));

        System.out.println("--------------------数组反转--------------------");
        System.out.println(Arrays.toString(oneDimensional));
        Collections.reverse(Arrays.asList(oneDimensional));
        System.out.println(Arrays.toString(oneDimensional));
        int[] reverseArray = new int[6];
        ArrayList arraylist = new ArrayList();
        for (int i = 0; i < 6; i++) {
            arraylist.add(oneDimensional[i]); //存放元素
        }
        Collections.reverse(arraylist); //使用方法进行逆序
        //完成逆序后,可以保存到新数组reverseArray
        for (int i = 0; i < 6; i++) {
            reverseArray[i] = (int) arraylist.get(i);
        }
        System.out.println(Arrays.toString(reverseArray));


        System.out.println("--------------------二纬数组字符串格式--------------------");
        System.out.println(Arrays.deepToString(twoDimensional));
    }
}
